package cn.itcast.web.controller.cargo;


import cn.itcast.domain.cargo.*;
import cn.itcast.service.cargo.ExportProductService;
import cn.itcast.service.cargo.ExportService;
import cn.itcast.service.cargo.FinanceService;
import cn.itcast.service.cargo.PackingService;
import cn.itcast.utils.BeanMapUtils;
import cn.itcast.utils.DownloadUtil;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("cargo/finance")
public class FinancePOIController extends BaseController {
    @Reference
    private FinanceService financeService;

    @Reference
    private PackingService packingService;

    @Reference
    private ExportProductService exportProductService;

    @Reference
    private ExportService exportService;


    @RequestMapping(value = "printExcel",name = "打印财务报运单")
    public void printExcel(String financeId, HttpServletResponse response) throws IOException {
        System.out.println(financeId);
        //1. 使用模版创建workbook
        Workbook wk = new HSSFWorkbook(session.getServletContext().getResourceAsStream("/make/xlsprint/tFINANCE.xls"));
        Finance finance = financeService.findById(financeId);
        Packing packing = packingService.findById(finance.getInvoiceId());


        System.out.println(packing.getExportId());
        Export export = exportService.findById(packing.getExportId());

        ExportProductExample exportProductExample = new ExportProductExample();
        ExportProductExample.Criteria criteria = exportProductExample.createCriteria();
        criteria.andExportIdEqualTo(packing.getExportId());






        //2. 获取第一页
        Sheet sheet = wk.getSheetAt(0);

        Row row = sheet.getRow(1);
        //制单日期
        Cell cell = row.getCell(3);
        cell.setCellValue(DateFormatUtils.format(finance.getInputDate(),"yyyy-MM-dd"));
        //发票号
        cell = row.getCell(15);
        cell.setCellValue(finance.getInvoiceId());

        row = sheet.getRow(2);
        //合同号
        cell = row.getCell(3);
        cell.setCellValue(finance.getExportNos());

        //信用号
        cell = row.getCell(11);
        cell.setCellValue(export.getLcno());

        row = sheet.getRow(3);

        //收货人及地址
        cell = row.getCell(3);
        cell.setCellValue(export.getConsignee());
        //备注
        cell = row.getCell(14);
        cell.setCellValue(export.getRemark());

        row = sheet.getRow(4);
        //装运港
        cell = row.getCell(2);
        cell.setCellValue(export.getShipmentPort());
        //目的港
        cell = row.getCell(5);
        cell.setCellValue(export.getDestinationPort());
        //FBO/CIF价格条件
        cell = row.getCell(10);
        cell.setCellValue(export.getPriceCondition());


        List<ExportProduct> list = exportProductService.findAll(exportProductExample);
        CellStyle[] cellStyle=new CellStyle[19];
        row = sheet.getRow(7);
        for (int i = 1; i < 19; i++) {
            cellStyle[i] = row.getCell(i).getCellStyle();
        }
        if (list!=null && list.size()>0) {
            int index = 7;
            for (ExportProduct exportProduct : list) {

                row = sheet.createRow(index);
                //货号
                cell = row.createCell(1);
                cell.setCellValue(exportProduct.getProductNo());
                cell.setCellStyle(cellStyle[1]);
                cell = row.createCell(2);
                cell.setCellStyle(cellStyle[2]);
                cell = row.createCell(3);
                cell.setCellStyle(cellStyle[3]);
                //厂家
                cell = row.createCell(4);
                cell.setCellValue(exportProduct.getFactoryName());

                cell.setCellStyle(cellStyle[4]);
                //单位
                cell = row.createCell(5);
                cell.setCellValue(exportProduct.getPackingUnit());
                cell.setCellStyle(cellStyle[5]);
                //数量
                cell = row.createCell(6);
                cell.setCellValue(exportProduct.getBoxNum());
                cell.setCellStyle(cellStyle[6]);
                //件数
                cell = row.createCell(7);
                cell.setCellValue(exportProduct.getCnumber());
                cell.setCellStyle(cellStyle[7]);
                //毛重
                cell = row.createCell(8);
                cell.setCellValue(exportProduct.getGrossWeight());
                cell.setCellStyle(cellStyle[8]);

                //净重
                cell = row.createCell(9);
                cell.setCellValue(exportProduct.getNetWeight());
                cell.setCellStyle(cellStyle[9]);
                //长
                cell = row.createCell(10);
                cell.setCellValue(exportProduct.getSizeLength());
                cell.setCellStyle(cellStyle[10]);
                //宽
                cell = row.createCell(11);
                cell.setCellValue(exportProduct.getSizeWidth());
                cell.setCellStyle(cellStyle[11]);
                //高
                cell = row.createCell(12);
                cell.setCellValue(exportProduct.getSizeHeight());
                cell.setCellStyle(cellStyle[12]);
                //出口单价
                cell = row.createCell(13);
                cell.setCellValue(exportProduct.getExPrice());
                cell.setCellStyle(cellStyle[13]);
                //出口金额
                cell = row.createCell(14);
                cell.setCellValue(exportProduct.getExPrice()*exportProduct.getCnumber());
                cell.setCellStyle(cellStyle[14]);
                //单价
                cell = row.createCell(15);
                cell.setCellValue(exportProduct.getPrice());
                cell.setCellStyle(cellStyle[15]);
                //金额
                cell = row.createCell(16);
                cell.setCellStyle(cellStyle[16]);
                //税金
                cell = row.createCell(17);
                cell.setCellStyle(cellStyle[17]);

                cell = row.createCell(18);
                cell.setCellStyle(cellStyle[18]);

                index++;

            }


        }

        //6. 写回浏览器
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        wk.write(bos);
        DownloadUtil.download(bos,response,financeId+"财务报运单.xls");
    }


    @RequestMapping(value="financePdf",name = "pdf下载")
    public void exportPdf(String financeId,HttpServletResponse response) throws JRException, IOException {
        //1.加载jasper文件
        InputStream is = session.getServletContext().getResourceAsStream("/jasper/finance.jasper");
        Finance finance = financeService.findById(financeId);
        Packing packing = packingService.findById(finance.getInvoiceId());


        System.out.println(packing.getExportId());
        Export export = exportService.findById(packing.getExportId());

        ExportProductExample exportProductExample = new ExportProductExample();
        ExportProductExample.Criteria criteria = exportProductExample.createCriteria();
        criteria.andExportIdEqualTo(packing.getExportId());

        List<ExportProduct> list = exportProductService.findAll(exportProductExample);

        Map map = BeanMapUtils.beanToMap(export);

        JasperPrint jasperPrint = JasperFillManager.fillReport(is, map, new JRBeanCollectionDataSource(list));
        //4.导出文件
        JasperExportManager.exportReportToPdfStream(jasperPrint,response.getOutputStream());
    }



}
